﻿using System;
using System.Web.Mvc;
using KLS.PPDS.Utilities;
using KLS.PPDS.Utility;
using KLS.PPDS.Web.Models;
using KLS.PPDS.ApplicationService.Manager;
using KLS.PPDS.Utilities.BaseController;

namespace KLS.PPDS.Web.Controllers.Company
{
    /// <summary>
    /// Handles all the request from Company Screen
    /// </summary>
    public class CompanyController : BaseController
    {
        //
        // GET: /Company/

        //override  

        private CompanyViewModel _model;

        /// <summary>
        /// return company List screen
        /// </summary>
        /// <returns></returns>
        
        [HttpGet]
        [AjaxAuthorize(Roles = RoleConstant.Superadmin)]
        public ActionResult Index()
        {
            //throw new Exception("ddd");
            ModelState.Clear();
            ViewData.Clear();
            _model = new CompanyViewModel();
            _model.CompanyList = CompanyManager.GetAllCompany(CommonSession.CompanyId);
            return PartialView(_model);
        }

        /// <summary>
        /// return company List screen
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        [AjaxAuthorize(Roles = RoleConstant.Superadmin)]
        public ActionResult CompnayList()
        {
            //throw new Exception("ddd");
            ModelState.Clear();
            ViewData.Clear();
            _model = new CompanyViewModel();
            _model.CompanyList = CompanyManager.GetAllCompany(CommonSession.CompanyId);
            return PartialView(_model);
        }



        /// <summary>
        /// returns CompanySettings Page for new company
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        [AjaxAuthorize(Roles = RoleConstant.Superadmin)]
        public ActionResult CompanySettings()
        {
            _model = new CompanyViewModel();
            _model.Company = new Core.Entity.Company();

            _model.Editors = new SelectList(UserManager.GetUsersByRoleName(CommonConstant.Editor, CommonSession.CompanyId), "Id", "UserName");
            
            _model.Graphics = new SelectList(UserManager.GetUsersByRoleName(CommonConstant.Graphics, null), "Id", "UserName");
            
            return PartialView(_model);
        }

        /// <summary>
        /// add or update a company to the database
        /// </summary>
        /// <param name="model">CompanyViewModel for retriving Copany Information</param>
        /// <returns></returns>
         
        [HttpPost]
        [AjaxAuthorize(Roles = RoleConstant.Superadmin)]
        public ActionResult SaveCompanySettings(CompanyViewModel model)
        {

            string oldCompanyFolderInPackage = "";
            string newCompanyFolderInPackage = "";
            var company = model.Company.Id==0 ? new Core.Entity.Company() : CompanyManager.GetCompany(int.Parse(model.Company.Id.ToString()));
            oldCompanyFolderInPackage = ArticleContentModel.GetPackageCompanyBaseFolder(company);


            // if company renamed
            newCompanyFolderInPackage = ArticleContentModel.GetPackageCompanyBaseFolder(model.Company);
            if (oldCompanyFolderInPackage != newCompanyFolderInPackage)
            {
                FileUtil.RemaneDirectory(oldCompanyFolderInPackage, newCompanyFolderInPackage);
            }

            company.CompanyName = model.Company.CompanyName;
            company.Editor = model.Company.Editor;
            company.Graphic = model.Company.Graphic;
            company.CreatedDate = DateTime.Now;



            //-----------------

            var msg = CompanyManager.Validate(company);

            if (String.IsNullOrEmpty(msg)) 
            {
                try
                {
                    CompanyManager.Save(company);
                }
                catch (Exception ex )
                {
                    if (oldCompanyFolderInPackage != newCompanyFolderInPackage)
                    {
                        FileUtil.RemaneDirectory(newCompanyFolderInPackage, oldCompanyFolderInPackage);
                    }
                    throw ex;
                }
                
                return RedirectToAction("CompnayList");
            }

            return JsonMessage(msg);

        }

        /// <summary>
        /// Return company settings for Edit company
        /// </summary>
        /// <param name="companyId">Company Id to Edit</param>
        /// <returns></returns>

        [HttpGet]
        [AjaxAuthorize(Roles = RoleConstant.Superadmin)]
        public ActionResult EditCompany(string companyId)
        {
            var cid = Convert.ToInt32(companyId);
            _model = new CompanyViewModel();
                        
            _model.Editors = new SelectList(UserManager.GetUsersByRoleName(CommonConstant.Editor, cid), "Id", "UserName");

            _model.Graphics = new SelectList(UserManager.GetUsersByRoleName(CommonConstant.Graphics, null), "Id", "UserName");

            _model.Company = CompanyManager.GetCompany(cid);

            return PartialView("~/Views/Company/CompanySettings.ascx", _model); 

        }

        /// <summary>
        /// Set IsDeleted flag true in the database
        /// </summary>
        /// <param name="companyId"> companyId to Delete</param>
        /// <returns></returns>

        [HttpGet]
        [AjaxAuthorize(Roles = RoleConstant.Superadmin)]
        public ActionResult DeleteCompany(string companyId)
        {
            CompanyManager.DeleteCompany(int.Parse(companyId));
            ModelState.Clear();

            _model = new CompanyViewModel();
            _model.CompanyList = CompanyManager.GetAllCompany(CommonSession.CompanyId);
            return PartialView("~/Views/Company/CompnayList.ascx", _model);

        }


    }
}
